<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/28
 * Time: 14:27
 */
namespace app\command\cron;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class runQuestionPlan extends Command{

    protected function configure()
    {
        $this->setName('runQuestionPlan')
            ->setDescription('执行获取题目任务');
    }

    /**
     * 建议每隔五秒执行一次
     * @param Input $input
     * @param Output $output
     * @return int|null|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function execute(Input $input, Output $output)
    {
        set_time_limit(17200);
        ini_set('memory_limit','5024M');
        set_time_limit(17200);
        ini_set('memory_limit','5024M');
        //获取哪个城市的
        $city = '杭州';
        $token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjE3eGMuY29tIiwiYXVkIjoiMTIxN3hjLmNvbSIsImlhdCI6MTY4NjEyMzU3OSwibmJmIjoxNjg2MTIzNTc5LCJ1c2VyIjp7ImlkXyI6IjE0NDUzIiwidXNlcl9uYW1lIjoiXHU1ZmFlXHU0ZmUxXHU3NTI4XHU2MjM3IiwidXNlcl9waG9uZSI6IjE4ODUyMTIyNTI5IiwidXNlcl9hdmF0YXIiOiJodHRwczpcL1wvdGhpcmR3eC5xbG9nby5jblwvbW1vcGVuXC92aV8zMlwvUE9nRXdoNG1JSE80bmliSDBLbE1FQ05qakd4UVVxMjRaRWFHVDRwb0M2aWNSaWNjVkdLU3lYd2liY1BxNEJXbWlhSUd1RzFpY3d4YVFYNmdyQzlWZW1ab0o4cmdcLzEzMiIsInd4dXNlcl9pZCI6IjE2MDgwIiwid3h1c2VyX3VuaW9uaWQiOiJvQTB1UnM3OFhiWDl2clZEdm5taFVNdTFCcjhBIiwidHR1c2VyX2lkIjpudWxsLCJ0dHVzZXJfdW5pb25pZCI6bnVsbCwicm9sZV8iOiIwIiwibG9jYXRpb25fIjoiXHU2NzZkXHU1ZGRlIiwic3RhdHVzXyI6IjEiLCJhZGRfdGltZSI6IjIwMjMtMDYtMDcgMTU6Mzk6MzciLCJsb25naXR1ZGVfIjpudWxsLCJsYXRpdHVkZV8iOm51bGx9fQ.swqbifz48Is_FEXvPqJ3x5W2X2HMBIRdteuOQssBmK0';

        /**********获取对应的试卷章节****************/
        $chapter_param['token'] = $token;
        $chapter_param['city'] = $city;
        $chapter_result = http_post_json('https://api.1217xc.com/exam2Weixin/getChapterList', $chapter_param);
        $chapter = json_decode($chapter_result, true)['data'];
        //删除对应的章节
        Db::name('chapter')
            ->where('city_',$city)
            ->delete();
        //删除对应的试题
        Db::name('question')
            ->where('city_',$city)
            ->delete();
        foreach ($chapter as $k=>$v) {
            $output->writeln("执行到第".($k+1).'章开始');
            $chapter_data['id_'] = $v['id_'];
            $chapter_data['cid_'] = $v['cid_'];
            $chapter_data['chaper_'] = $v['chaper_'];
            $chapter_data['total_'] = $v['total_'];
            $chapter_data['judge_num'] = $v['judge_num'];
            $chapter_data['single_sel_num'] = $v['single_sel_num'];
            $chapter_data['muti_sel_num'] = $v['muti_sel_num'];
            $chapter_data['fill_blank_num'] = $v['fill_blank_num'];
            $chapter_data['pid_'] = $v['pid_'];
            $chapter_data['city_'] = $city;
            $chapter_data['update_at'] = currentTime();
            $chapter_data['create_at'] = currentTime();
            Db::name('chapter')->insert($chapter_data);
            $param['token'] = $token;
            if ($v['city_'] == '0') {
                $param['city'] = 'national';
            } else {
                $param['city'] = $v['city_'];
            }
            $param['chapter'] = $v['cid_'];
            $param['location'] = '上海';
            $result = http_post_json('https://api.1217xc.com/exam2Weixin/getExamListByType', $param);
            $result = json_decode($result, true);
            if (isset($result['data']['list'])) {
                $list = $result['data']['list'];
                $temp = [];
                foreach ($list as $kk=>$vv){
                    $output->writeln("执行到第".($kk+1).'题');
                    //通过接口获取对应的题目详情
                    $url1 = 'https://api.1217xc.com/exam2Weixin/getQuestionById';
                    $param1['token'] = $token;
                    $param1['qid'] = $vv['id_'];
                    $result1 = http_post_json($url1,$param1);
                    $result1 = json_decode($result1,true);
                    $data = $result1['data'];
                    $temp[$kk]['id_'] = $data['id_'];
                    $temp[$kk]['no_'] = $data['no_'];
                    $temp[$kk]['type_'] = $data['type_'];
                    $temp[$kk]['chapter_'] = $data['chapter_'];
                    $temp[$kk]['question_'] = $data['question_'];
                    $temp[$kk]['an1_'] = $data['an1_'];
                    $temp[$kk]['an2_'] = $data['an2_'];
                    $temp[$kk]['an3_'] = $data['an3_'];
                    $temp[$kk]['an4_'] = $data['an4_'];
                    $temp[$kk]['an5_'] = $data['an5_'];
                    $temp[$kk]['an6_'] = $data['an6_'];
                    $temp[$kk]['an7_'] = $data['an7_'];
                    $temp[$kk]['an8_'] = $data['an8_'];
                    $temp[$kk]['an9_'] = $data['an9_'];
                    $temp[$kk]['an10_'] = $data['an10_'];
                    $temp[$kk]['answer_true'] = $data['answer_true'];
                    $temp[$kk]['city_'] = $city;
                    $temp[$kk]['explain_'] = $data['explain_'];
                    $temp[$kk]['img_url'] = $data['img_url'];
                    $temp[$kk]['video_url'] = $data['video_url'];
                    $temp[$kk]['video_text'] = $data['video_text'];
                    $temp[$kk]['an1_pic'] = $data['an1_pic'];
                    $temp[$kk]['an2_pic'] = $data['an2_pic'];
                    $temp[$kk]['an3_pic'] = $data['an3_pic'];
                    $temp[$kk]['an4_pic'] = $data['an4_pic'];
                    $temp[$kk]['is_fav'] = $data['is_fav'];
                    $temp[$kk]['is_fav'] = $data['is_fav'];
                }
                Db::name('question')->insertAll($temp);
            }
            $output->writeln("执行到第".($k+1).'章结束');
        }
        //最后删除用户的答题记录、浏览记录、收藏记录、错题记录
        Db::name('user_answer_error')
            ->where('city',$city)
            ->delete();
        Db::name('user_answer_record')
            ->where('city',$city)
            ->delete();
        Db::name('user_collect')
            ->where('city',$city)
            ->delete();
        Db::name('user_view_record')
            ->where('city',$city)
            ->delete();
        $output->writeln("计划任务执行完成");
    }
}